package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class SpringRabbitListener {
    public int count1=0;
    public int count2=0;
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        log.warn("消费者1接收到消息{}，{}",msg,++count1);
        Thread.sleep(20);
    }
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        log.warn("消费者2接收到消息{}，{}",msg,++count2);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) throws InterruptedException {
        log.warn("消费者1接收到消息{}，{}",msg,++count1);
//        Thread.sleep(20);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) throws InterruptedException {
        log.warn("消费者2接收到消息{}，{}",msg,++count2);
//        Thread.sleep(200);
    }

    @RabbitListener(
            bindings = @QueueBinding(value = @Queue("direct.queue1"),
            exchange = @Exchange(name = "my.direct",type = ExchangeTypes.DIRECT),key = {"red","blue"})
                    )
    public void listenDirectQueue1(String msg){
        log.warn("1号消费者的消息为{}",msg);
    }

    @RabbitListener(
            bindings = @QueueBinding(value = @Queue("direct.queue2"),
            exchange = @Exchange(name = "my.direct",type = ExchangeTypes.DIRECT),key = {"red","yellow"})
                    )
    public void listenDirectQueue2(String msg){
        log.warn("2号消费者的消息为{}",msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue1"),
                    exchange = @Exchange(name = "my.topic", type = ExchangeTypes.TOPIC),key = {"China.#"}))
    public void listenTopicQueue1(String msg){
        log.warn("1号消费者的消息为{}",msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue2"),
                    exchange = @Exchange(name = "my.topic", type = ExchangeTypes.TOPIC),key = {"Japanese.#"}))
    public void listenTopicQueue2(String msg){
        log.warn("2号消费者的消息为{}",msg);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue3"),
                    exchange = @Exchange(name = "my.topic", type = ExchangeTypes.TOPIC),key = {"*.news"}))
    public void listenTopicQueue3(String msg){
        log.warn("3号消费者的消息为{}",msg);
    }
}
